using System;
using System.Reflection;
using NUnit.Framework;
using ODX.Core;
using ODX.LazyUnitTester;

namespace SavePoints
{
    [Table("Human")]
    public abstract class Human : Entity
    {
        public abstract string Name { get; set;}
        public abstract int Age { get; set;}

        [Column("MasterID")]
        public abstract IEntityList<Pet> Pets { get; }
    }

    [Table("Pet")]
    public abstract class Pet : Entity
    {
        public abstract string Name { get; set;}
        public abstract Human Master { get; set; }
    }

    [TestFixture]
    public class C06_SavePoints
    {
        [TestBody]
        static void Main()
        {
            Session session = new Session();
            session.RegisterAssembly(Assembly.GetExecutingAssembly());
            session.Prepare();

            Human john = session.Create<Human>();
            john.Name = "John";
            john.Age = 23;

            Pet cat = session.Create<Pet>();
            cat.Name = "Molly";
            cat.Master = john;

            DescribeHuman(john);

            session.SavePoint();

            john.Age++;

            Pet dog = session.Create<Pet>();
            dog.Name = "Beethoven";

            dog.Master = john;

            DescribeHuman(john);

            session.RestorePoint();

            DescribeHuman(john);
        }

        private static void DescribeHuman(Human john)
        {
            LUT.WriteLine("Now {0} is {1}. He masters", john.Name, john.Age);
            foreach (Pet pet in john.Pets)
                LUT.WriteLine("\t{0}", pet.Name);
            LUT.WriteLine();
            LUT.WriteLine("------------------------");
        }

        [Test]
        public void Test()
        {
            LUT.Execute(Main);
        }
    }
}
